package com.practise.coffee.util;

import com.practise.coffee.dao.MakeDao;
import com.practise.coffee.enums.CoffeeType;

import java.util.HashMap;
import java.util.Map;
import java.util.ServiceLoader;

/**
 * @author:lhz
 * @date:2019/2/18 10:12
 */
public class ClassUtil {

	private static Map<CoffeeType, MakeDao> coffeeDaoMap = new HashMap();

	static {
		/**
		 * 需要在resources中META-INF.services配置对应的接口实现类
		 * https://www.jianshu.com/p/7601ba434ff4
		 */
		ServiceLoader<MakeDao> serviceLoader = ServiceLoader.load(MakeDao.class);

		for (MakeDao myServiceLoader : serviceLoader) {
//			System.out.println("type: " + myServiceLoader.type());
			coffeeDaoMap.put(myServiceLoader.type(), myServiceLoader);
		}
	}

	public static MakeDao getDao(CoffeeType coffeeType) {
		return coffeeDaoMap.get(coffeeType);
	}

	public static void main(String[] args) {
		getDao(CoffeeType.MOCHA);
	}
}
